package com.fog.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fog.student.common.UserContext;
import com.fog.student.dto.CourseDto;
import com.fog.student.entity.*;
import com.fog.student.mapper.CourseMapper;
import com.fog.student.service.AttendanceService;
import com.fog.student.service.CourseService;
import com.fog.student.service.SelectedCourseService;
import com.fog.student.service.TeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Resource
    private TeacherService teacherService;

    @Resource
    private SelectedCourseService selectedCourseService;

    @Resource
    private CourseService courseService;

    @Resource
    private AttendanceService attendanceService;
    @Override
    @Transactional
    public Page<CourseDto> pageSearch(int page, int pageSize, String name) {
        Page<Course> pageInfo = new Page<>(page, pageSize);
        // 返回页面的分页
        Page<CourseDto> courseDtoPage = new Page<>();

        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();

        courseQueryWrapper.orderByDesc("id");
        if(name !=null){
            courseQueryWrapper.like("name", name);
        }
        // 查出信息
        page(pageInfo, courseQueryWrapper);
        // 拷贝信息
        BeanUtils.copyProperties(pageInfo, courseDtoPage);

        // 根据id查询
        List<Course> records = pageInfo.getRecords();
        List<CourseDto> recordsDto = records.stream().map((item)->{
            CourseDto courseDto = new CourseDto();
            BeanUtils.copyProperties(item, courseDto);
            String teacherName = teacherService.getById(item.getTeacherId()).getName();
            courseDto.setTeacherName(teacherName);
            return courseDto;
        }).collect(Collectors.toList());
        // 重新放入到 返回集合中
        courseDtoPage.setRecords(recordsDto);
        return courseDtoPage;
    }

    @Override
    @Transactional
    public Page<CourseDto> selectSearch(int page, int pageSize, String name) {
        Page<Course> pageInfo = new Page<>(page, pageSize);
        // 返回页面的分页
        Page<CourseDto> courseDtoPage = new Page<>();

        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();

        courseQueryWrapper.orderByDesc("id");

        if(name !=null){
            courseQueryWrapper.like("name", name);
        }
        String id = UserContext.getUserId().toString();
        courseQueryWrapper.inSql("id","select course_id from selected_course where student_id = "+ id);
        // 查出信息
        page(pageInfo, courseQueryWrapper);
        // 拷贝信息
        BeanUtils.copyProperties(pageInfo, courseDtoPage);

        // 根据id查询
        List<Course> records = pageInfo.getRecords();
        List<CourseDto> recordsDto = records.stream().map((item)->{
            CourseDto courseDto = new CourseDto();
            BeanUtils.copyProperties(item, courseDto);
            String teacherName = teacherService.getById(item.getTeacherId()).getName();
            courseDto.setTeacherName(teacherName);
            return courseDto;
        }).collect(Collectors.toList());
        // 重新放入到 返回集合中
        courseDtoPage.setRecords(recordsDto);
        return courseDtoPage;
    }

    @Override
    @Transactional
    public Page<CourseDto> noSelectSearch(int page, int pageSize, String name) {
        Page<Course> pageInfo = new Page<>(page, pageSize);
        // 返回页面的分页
        Page<CourseDto> courseDtoPage = new Page<>();

        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();

        courseQueryWrapper.orderByDesc("id");
        if(name !=null){

            courseQueryWrapper.like("name", name);
        }
        String id = UserContext.getUserId().toString();
        courseQueryWrapper.inSql("id","select id FROM course where id NOT IN (select course_id from selected_course where student_id = "+ id+")");
        // 查出信息
        page(pageInfo, courseQueryWrapper);
        // 拷贝信息
        BeanUtils.copyProperties(pageInfo, courseDtoPage);

        // 根据id查询
        List<Course> records = pageInfo.getRecords();
        List<CourseDto> recordsDto = records.stream().map((item)->{
            CourseDto courseDto = new CourseDto();
            BeanUtils.copyProperties(item, courseDto);
            String teacherName = teacherService.getById(item.getTeacherId()).getName();
            courseDto.setTeacherName(teacherName);
            return courseDto;
        }).collect(Collectors.toList());
        // 重新放入到 返回集合中
        courseDtoPage.setRecords(recordsDto);
        return courseDtoPage;
    }

    @Override
    @Transactional
    public Page<Course> selectTeacherCourse(int page, int pageSize, String name) {
        Page<Course> pageInfo = new Page<>(page, pageSize);

        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();

        courseQueryWrapper.orderByDesc("id");

        if(name !=null){
            courseQueryWrapper.like("name", name);
        }
        String id = UserContext.getUserId().toString();
        courseQueryWrapper.eq("teacher_id",id);
        // 查出信息
        courseService.page(pageInfo,courseQueryWrapper);

        return pageInfo;
    }

    @Override
    public void deleteCourse(String ids) {
        String[] list = ids.split(",");
        for (String id : list) {
            // 删除课程相应信息
            selectedCourseService.remove(new QueryWrapper<SelectedCourse>().eq("course_id", id));
            attendanceService.remove(new QueryWrapper<Attendance>().eq("course_id", id));
            // 删除课程自身信息
            removeById(Integer.parseInt(id));
        }
    }

    @Override
    @Transactional
    public void addCourse(CourseDto courseDto) {

        int teacherId = Integer.parseInt(courseDto.getTeacherName());

        Course course = new Course();

        course.setTeacherId(teacherId);

        course.setInfo(courseDto.getInfo());

        course.setCourseDate(courseDto.getCourseDate());

        course.setMaxNum(courseDto.getMaxNum());

        course.setName(courseDto.getName());

        course.setSelectedNum(0);

        courseService.save(course);
    }
}
